﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SCABaselineCS.Tests.Reliability
{
    [TestClass]
    public class IDisposableTests
    {
        public class ExampleClass : IDisposable
        {
            public bool Disposed { get; private set; }
            public void Dispose() { this.Disposed = true; }
        }

        
        [TestMethod]
        public void Example_Fail()
        {
            // Arrange
            ExampleClass x = null;

            // Act
            x = new ExampleClass();
            // do not dispose

            // Assert
            Assert.IsFalse(x.Disposed);
        }

        [TestMethod]
        public void Example_Pass_Using()
        {
            // Arrange
            ExampleClass x = null;

            // Act
            using (x = new ExampleClass())
            { /* do nothing */ }

            // Assert
            Assert.IsTrue(x.Disposed);
        } // Example_Pass_Using

        [TestMethod]
        public void Example_Pass_TryCatch()
        {
            // Arrange
            ExampleClass x = null;

            // Act
            try
            {
                x = new ExampleClass();
            }
            finally
            {
                if (x != null) x.Dispose();
            }

            // Assert
            Assert.IsTrue(x.Disposed);
        } // Example_Pass_TryCatch

    }
}
